/**
 * Created by duwei on 2019/3/28.
 */

var ZiPai_JieSuanJavaWindow_qylds = JieSuanWindow.extend({
    _ccsFileName: zipai.res.ZiPai_JieSuanWindow_qylds_json,
    /**
     * 胡牌类型
     */
    HU_TYPE: {
        LIUJU: 0,
        ZIMO: 1,
        DIANPAO: 2,
        JIESAN: 3
    },
    PAI_TYPE:['','','吃','啸','碰','啸','啸','倾','跑'],
    WINNER_LOCATION : {
        WU : 0,
        XIAJIA : 1,
        DUIJIA : 2,
        SHANGJIA : 3
    },
    _cardSize: 42,
    name2img:{
        "吃" : "chi.png",
        "底牌" : "end_dipai.png",
        "倾" : "qing.png",
        "啸" : "xiao.png",
        "跑" : "pao.png",
        "碰" : "peng.png",
        "番数" : "fenshu_fanshu.png",
        "胡息" : "fenshu_huxi.png",
        "囤数" : "fenshu_tunshu.png",
        "总囤数" : "fenshu_zongtun.png",
        "点胡" : "hupai_dianhu.png",
        "点炮" : "hupai_dianpao.png",
        "翻醒" : "hupai_fanxing.png",
        "跟醒" : "hupai_genxing.png",
        "黑胡" : "hupai_heihu.png",
        "红胡" : "hupai_honghu.png",
        "平胡" : "hupai_pinghu.png",
        "全红" : "hupai_quanhu.png",
        "天胡" : "hupai_tianhu.png",
        "王闯" : "hupai_wangchuang.png",
        "王钓" : "hupai_wangdiao.png",
        "自摸" : "hupai_zimo.png"
    },
    /**
     * 初始化界面
     * @private
     */
    _initInfo: function () {
        var self = this;
        self.isZimo = (self._data['hu_type'] == self.HU_TYPE.ZIMO);
        self.isLiuju = (self._data['hu_type'] == self.HU_TYPE.LIUJU);
        self.isDianpao = (self._data['hu_type'] == self.HU_TYPE.DIANPAO);
        self.isJiesan = (self._data['hu_type'] == self.HU_TYPE.JIESAN);
        self.$ = create$(this.getRootNode());
        if (self.isLiuju || self.isJiesan){
            self._hidePlayerData();
        }
        else {
            self._setRoomId();
            self._setPlayer();
            self._setDipai();
        }
        self._setTitle();
        self._setWanFa();
        self._setPlayerInfo();
        self._setBtn();
    },
    getRootNode: function () {
        return this._nodeList['root'];
    },
    /**
     * 设置房间号/局数
     */
    _setRoomId: function () {
        var self = this;
        var roomIdStr = '房号：' + gameData.roomId;
        var roomJSStr = '局数：' + gameData.curRound + '/' + gameData.totalRound;
        self.$("PanelResult.txt_roomid").setString(roomIdStr);
        self.$("PanelResult.txt_jushu").setString(roomJSStr);
    },
    /**
     * 设置玩法名称
     * @private
     */
    _setWanFa: function () {
        var self = this;
        self.$("PanelResult.txt_wanfa").setString(gameData.wanfaDesp);
    },
    /**
     * 设置标题
     * @private
     */
    _setTitle: function () {
        var self = this;
        var title = self.$("title");
        var shengli = self.$('shengli', title);
        var shibai = self.$('shibai', title);
        var huangzhuang = self.$('huangzhuang', title);
        huangzhuang.setVisible(self.isLiuju);
        var playerData = {};
        var playersData = self._data['players'];
        for (var i = 0; i < playersData.length; i++){
            playerData = playersData[i];
            if (playerData['hu']){
                self.Winner = playerData['uid'];
            }
        }
        var isWiner = self.Winner == gameData.uid;
        shengli.setVisible(isWiner);
        var isShowShibai = self.isJiesan || !isWiner;
        shibai.setVisible(isShowShibai);
        //显示败家对应赢家的位置
        if (isShowShibai){
            if (self.winnerLocation == self.WINNER_LOCATION.DUIJIA){
                self.$('shangjiasheng', shibai).setVisible(false);
                self.$('duijiasheng', shibai).setVisible(true);
                self.$('xiajiasheng', shibai).setVisible(false);
            }
            else if (self.winnerLocation == self.WINNER_LOCATION.XIAJIA) {
                self.$('shangjiasheng', shibai).setVisible(false);
                self.$('duijiasheng', shibai).setVisible(false);
                self.$('xiajiasheng', shibai).setVisible(true);
            }
            else if (self.winnerLocation == self.WINNER_LOCATION.SHANGJIA) {
                self.$('shangjiasheng', shibai).setVisible(true);
                self.$('duijiasheng', shibai).setVisible(false);
                self.$('xiajiasheng', shibai).setVisible(false);
            }
            else {
                self.$('shangjiasheng', shibai).setVisible(false);
                self.$('duijiasheng', shibai).setVisible(false);
                self.$('xiajiasheng', shibai).setVisible(false);
            }
        }
    },
    /**
     * 设置单人数据
     * @private
     */
    _setPlayer:function(){
        var self = this;
        var playersData = self._data['players'];
        var pCount = playersData.length;
        var playerData = {};
        //查找赢家
        var wineri = 0;
        var curPlayeri = 0;
        for (var i = 0; i < pCount; i++){
            playerData = playersData[i];
            if (playerData['hu']){
                self.Winner = playerData['uid'];
                wineri = i;
            }
            if (playerData['uid'] == gameData.uid){
                curPlayeri = i;
            }
        }
        //链表
        function Node(element){
            this.element = element;
            this.prev = null;
            this.next = null;
        }
        var arrL = [];
        for (var i = 0; i < pCount; i++){
            arrL[i] = new Node(i);
        }
        for (var i = 0; i < pCount; i++) {
            if (i == 0){
                arrL[i].prev = arrL[pCount - 1];
                arrL[i].next = arrL[1];
            }
            else if (i == pCount - 1){
                arrL[i].prev = arrL[i - 1];
                arrL[i].next = arrL[0];
            }
            else{
                arrL[i].prev = arrL[i - 1];
                arrL[i].next = arrL[i + 1];
            }
        }

        //查找赢家相对于当前玩家的顺序
        if (self.Winner == gameData.uid){
            self.winnerLocation = self.WINNER_LOCATION.WU
        }
        else if (pCount == 2){
            self.winnerLocation = self.WINNER_LOCATION.DUIJIA;
        }
        else if(pCount == 3){
            if (arrL[curPlayeri].next.element == wineri) self.winnerLocation = self.WINNER_LOCATION.XIAJIA;
            else if (arrL[curPlayeri].prev.element == wineri) self.winnerLocation = self.WINNER_LOCATION.SHANGJIA;
        }
        else if(pCount == 4){
            if (arrL[curPlayeri].next.element == wineri) self.winnerLocation = self.WINNER_LOCATION.XIAJIA;
            else if (arrL[curPlayeri].prev.element == wineri) self.winnerLocation = self.WINNER_LOCATION.SHANGJIA;
            else self.winnerLocation = self.WINNER_LOCATION.DUIJIA;
        }
        self._setMidInfo();
        self._setLeftInfo();
        self._setRightInfo();
    },
    /**
     * 设置显示信息左侧的数据
     * @private
     */
    _setLeftInfo : function(){
        var self = this;
        var left = self.$("container_data.container_personal.left");
        var list = self.$("list", left);
        var xingpaiNode = self.$("xingpaiNode", left);
        var xingNode = self.$("xingNode", left);
        var huOpt = self.$("huOpt", left);
        list.removeAllChildren();

        var hupaiId = self._data.hu_pai_id;
        var xingpaiId = self._data.xingPaiId;
        var fanInfo = self._data.fanInfo;
        var laizi = self._data.laizi;
        var xingCount = self._data.xingPaiCnt;
        //查找赢家
        var playerData = {};
        for (var i = 0; i < self._data.players.length; i++){
            if (playerData['hu']){
                playerData = self._data.players[i];
                break;
            }
        }
        var xingType = gameData.options.xingType == undefined ? 0 : gameData.options.xingType;

        var dataArr = [];

        //王牌转换
        for (var i = 0; i < laizi.length; i++){
            dataArr[i] = {};
            dataArr[i].nodeType = 1;
            dataArr[i].pai1 = 21;
            dataArr[i].pai2 = laizi[i];
        }

        //醒牌
        if (xingType == 1 || xingType == 2) {
            dataArr[dataArr.length] = {};
            dataArr[dataArr.length - 1].nodeType = 2;
            dataArr[dataArr.length - 1].xingType = xingType;
            dataArr[dataArr.length - 1].pai1 = xingpaiId;
            dataArr[dataArr.length - 1].txt1 = xingCount;
        }
        if (fanInfo.length == 0){
            dataArr[dataArr.length] = {};
            dataArr[dataArr.length - 1].nodeType = 3;
            dataArr[dataArr.length - 1].txt1 = "平胡";
            dataArr[dataArr.length - 1].txt2 = 1;
        }
        else {
            for (var i = 0; i < fanInfo.length; i++) {
                var fan = fanInfo[i];
                for (var key in fan) {
                    dataArr[dataArr.length] = {};
                    dataArr[dataArr.length - 1].nodeType = 3;
                    dataArr[dataArr.length - 1].txt1 = decodeURIComponent(key);
                    dataArr[dataArr.length - 1].txt2 = fan[key];
                }
            }
        }

        //生成对应的数据
        var singData;
        for (var i = 0; i < dataArr.length; i++) {
            singData = dataArr[i];
            var custom_item = new ccui.Layout();
            custom_item.setContentSize(cc.size(284, 50));
            list.pushBackCustomItem(custom_item);
            var cloneObj;
            if (singData.nodeType == 1){
                cloneObj = duplicateNode(xingpaiNode);
                var card1 = new Card();
                card1.setData(singData.pai1, null, 2);
                cloneObj.getChildByName("img_21").addChild(card1);
                card1.setAnchorPoint(0.5, 0.5);
                var card2 = new Card();
                card2.setData(singData.pai2, null, 2);
                cloneObj.getChildByName("img_x").addChild(card2);
                card2.setAnchorPoint(0.5, 0.5);
            }
            else if (singData.nodeType == 2){
                cloneObj = duplicateNode(xingNode);
                cloneObj.getChildByName("xingName").setString(singData.xingType == 1? "跟醒" : "翻醒");
                var card1 = new Card();
                card1.setData(singData.pai1, null, 2);
                cloneObj.getChildByName("img_x").addChild(card1);
                card1.setAnchorPoint(0.5, 0.5);
                cloneObj.getChildByName("score").setString("+" + singData.txt1);
            }
            else if (singData.nodeType == 3){
                cloneObj = duplicateNode(huOpt);
                cloneObj.getChildByName("huType").setString(singData.txt1);
                cloneObj.getChildByName("score").setString("*" + singData.txt2);
            }
            if (singData.nodeType == 1 || singData.nodeType == 2 || singData.nodeType == 3) {
                custom_item.addChild(cloneObj);
                cloneObj.setPosition(cc.p(142, 25));
            }
        }
        xingpaiNode.setVisible(false);
        xingNode.setVisible(false);
        huOpt.setVisible(false);
    },
    /**
     * 设置显示信息中间的数据
     * @private
     */
    _setMidInfo : function() {
        var self = this;
        var mid = self.$("container_data.container_personal.mid");
        var player;
        var players = self._data['players'];
        for (var i = 0; i < players.length; i++) {
            if (players[i]['hu']) {
                player = players[i];
                break;
            }
        }
        var Menzi = player['duiArr'];
        var MenziLen = Menzi.length;
        Menzi.sort(function (a, b) {
            return b['pai_arr'].length - a['pai_arr'].length;
        });

        var huPlayerId = player.uid;
        var huOpenCards = deepCopy(self._room.zipaiManager.g_uid_to_opencards[huPlayerId]);


        var posNodeArr = self.getComboPosNode(Menzi);
        var tmpVal = self.handleMenzi(Menzi, huOpenCards);
        Menzi = tmpVal[0];
        var wangfanLocation = tmpVal[1];
        self.existHuDui = false;

        //检测王钓、王闯、王炸的位置

        for (var i = MenziLen - 1; i > -1; i--) {
            if (wangfanLocation > -1){
                if (wangfanLocation == i) {
                    self.setComBoInfo(posNodeArr[i], Menzi[i], self._data.hu_pai_id, wangfanLocation, true);
                }
                else {
                    self.setComBoInfo(posNodeArr[i], Menzi[i], self._data.hu_pai_id, wangfanLocation, false);
                }
            }
            else {
                self.setComBoInfo(posNodeArr[i], Menzi[i], self._data.hu_pai_id, wangfanLocation, false, self.isXingpaiId2HupaiId);
            }
        }
    },
    /**
     * 将门子中的赖子转换的牌转成王霸
     * @param menzi
     * @param huOpencards
     */
    handleMenzi : function(menzi, huOpencards){
        var self = this;

        //查询亮牌区的牌
        var tmpOpenArr = {};
        var tmpMenzi = {};
        var sameNum = 0;

        for (var i = 0; i < menzi.length; i++){
            menzi[i].isOpenCards = false;
        }

        for (var i = 0; i < huOpencards.length; i++){
            tmpOpenArr = deepCopy(huOpencards[i].pai_arr);
            tmpOpenArr.sort(function (a, b) {
                return a - b;
            })
            for (var j = 0; j < menzi.length; j++){
                tmpMenzi = deepCopy(menzi[j].pai_arr);
                if (tmpOpenArr.length == tmpMenzi.length && menzi[j].isOpenCards == false){
                    sameNum = 0;
                    tmpMenzi.sort(function (a, b) {
                        return a - b;
                    })
                    for (var m = 0; m < tmpOpenArr.length; m++){
                        if (tmpOpenArr[m] == tmpMenzi[m]) sameNum++;
                    }
                    if (sameNum == tmpOpenArr.length){
                        menzi[j].isOpenCards = true;
                        break;
                    }
                }
            }
        }


        //0无王fan 1.王钓 2.王闯 3.王炸
        var wangfanType = 0;
        var fanInfo = self._data.fanInfo;
        var hupaiId = self._data.hu_pai_id;
        var xingPaiId = self._data.xingPaiId;
        for (i = 0; i < fanInfo.length; i++){
            for (item in fanInfo[i]){
                if (item == "王钓"){
                    wangfanType = 1;
                    break;
                }
                else if (item == "王闯"){
                    wangfanType = 2;
                    break;
                }
                else if (item == "王炸"){
                    wangfanType = 3;
                    break;
                }
                else if (item == "王钓王"){
                    wangfanType = 11;
                    break;
                }
                else if (item == "王闯王"){
                    wangfanType = 12;
                    break;
                }
                else if (item == "王炸王"){
                    wangfanType = 13;
                    break;
                }
            }
        }

        //删除王番对应的牌，并记录对应的牌对对应的下标，不尽兴王番牌对的处理
        var retMenzi = deepCopy(menzi);
        var laizi = deepCopy(self._data.laizi);
        var wangfanLocation = -1;
        if (wangfanType > 0){
            if (wangfanType == 1){
                for (var i = 0; i < retMenzi.length; i++){
                    if (retMenzi[i].pai_arr.length == 2 && retMenzi[i].isHuDui){
                        if (retMenzi[i].pai_arr[0] == hupaiId) {
                            retMenzi[i].pai_arr[0] = 21;
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            wangfanLocation = i;
                            break;
                        }
                    }
                }
            }
            else if (wangfanType == 2){
                for (var i = 0; i < retMenzi.length; i++){
                    if (retMenzi[i].pai_arr.length == 3 && retMenzi[i].isHuDui){
                        if (retMenzi[i].pai_arr[0] == retMenzi[i].pai_arr[1] && retMenzi[i].pai_arr[0] == retMenzi[i].pai_arr[2]
                            && retMenzi[i].pai_arr[0] == hupaiId) {
                            retMenzi[i].pai_arr[0] = 21;
                            retMenzi[i].pai_arr[1] = 21;
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            wangfanLocation = i;
                            break;
                        }
                    }
                }
            }
            else if (wangfanType == 3){
                for (var i = 0; i < retMenzi.length; i++) {
                    if (retMenzi[i].pai_arr.length == 4 && retMenzi[i].isHuDui) {
                        if (retMenzi[i].pai_arr[0] == hupaiId) {
                            retMenzi[i].pai_arr[0] = 21;
                            retMenzi[i].pai_arr[1] = 21;
                            retMenzi[i].pai_arr[2] = 21;
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            laizi.splice(laizi.indexOf(hupaiId),1);
                            wangfanLocation = i;
                            break;
                        }
                    }
                }
            }
            else if (wangfanType == 11){
                for (var i = 0; i < retMenzi.length; i++){
                    if (retMenzi[i].pai_arr.length == 2 && retMenzi[i].isHuDui) {
                        retMenzi[i].pai_arr[0] = 21;
                        retMenzi[i].pai_arr[1] = 21;
                        laizi.splice(laizi.indexOf(xingPaiId), 1);
                        laizi.splice(laizi.indexOf(xingPaiId), 1);
                        wangfanLocation = i;
                        break;
                    }
                }
            }
            else if (wangfanType == 12){
                for (var i = 0; i < retMenzi.length; i++){
                    if (retMenzi[i].pai_arr.length == 3 && retMenzi[i].isHuDui) {
                        if (retMenzi[i].pai_arr[0] == xingPaiId && retMenzi[i].pai_arr[1] == xingPaiId && retMenzi[i].pai_arr[2] == xingPaiId) {
                            retMenzi[i].pai_arr[0] = 21;
                            retMenzi[i].pai_arr[1] = 21;
                            retMenzi[i].pai_arr[2] = 21;
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            wangfanLocation = i;
                            break;
                        }
                    }
                }
            }
            else if (wangfanType == 13){
                for (var i = 0; i < retMenzi.length; i++) {
                    if (retMenzi[i].pai_arr.length == 4 && retMenzi[i].isHuDui) {
                        if (retMenzi[i].pai_arr[0] == xingPaiId) {
                            retMenzi[i].pai_arr[0] = 21;
                            retMenzi[i].pai_arr[1] = 21;
                            retMenzi[i].pai_arr[2] = 21;
                            retMenzi[i].pai_arr[3] = 21;
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            laizi.splice(laizi.indexOf(xingPaiId), 1);
                            wangfanLocation = i;
                            break;
                        }
                    }
                }
            }
        }

        //判断王钓、王闯、王炸
        var isWangFanType = laizi.length < self._data.laizi.length ? true : false;

        //处理非王钓、王闯、王炸， 摸到王，王变成醒牌后， 胡对为当前醒牌
        self.isXingpaiId2HupaiId = false;
        var xingPaiId = self._data.xingPaiId;

        for (var i = 0; i < laizi.length; i++) {
            laizi[i] = {val : laizi[i], valid : true};
        }

        function replaceLaiziWithType2(Type2Dui) {
            var mz = Type2Dui.val;
            var isTrigger = false;
            if (mz.type == 2) {
                for (var i = 0; i < mz.pai_arr.length; i++) {
                    for (var j = 0; j < laizi.length; j++) {
                        if (laizi[j].valid && laizi[j].val == mz.pai_arr[i]) {
                            laizi[j].valid = false;
                            mz.pai_arr[i] = 21;
                            if (isWangFanType == false && self.isXingpaiId2HupaiId == false && laizi[j].val == xingPaiId) {
                                self.isXingpaiId2HupaiId = true;
                                mz.isXingpaiId2HupaiId = true;
                            }

                            isTrigger = true;
                            break;
                        }
                    }
                    if (isTrigger == true) break;
                }
            }
        }

        var tmpType2Arr = [];
        function findPaiduiHasLaiziCountWithTypes(mz) {
            if (mz.type == 2) {
                var count = 0;
                for (var i = 0; i < mz.pai_arr.length; i++) {
                    for (var j = 0; j < laizi.length; j++) {
                        if (laizi[j].valid && laizi[j].val == mz.pai_arr[i]) {
                            count++;
                        }
                    }
                }
                tmpType2Arr.push({val: mz, cnt: count});
            }
        }
        for (var i = retMenzi.length -1; i >= 0; i--){
            if (wangfanLocation == i) continue;
            if (retMenzi[i].isOpenCards == true) continue;
            findPaiduiHasLaiziCountWithTypes(retMenzi[i]);
        }

        tmpType2Arr.sort(function (a,b) {
            return a.cnt - b.cnt;
        });

        for (var i = 0; i < tmpType2Arr.length; i++){
            replaceLaiziWithType2(tmpType2Arr[i]);
        }

        function replaceLaiziWithoutType2(mz) {
            if (mz.type == 2){
                return;
            }
            else {
                for (var i = 0; i < mz.pai_arr.length; i++) {
                    for (var j = 0; j < laizi.length; j++) {
                        if (laizi[j].valid && laizi[j].val == mz.pai_arr[i]) {
                            laizi[j].valid = false;
                            mz.pai_arr[i] = 21;
                            if (isWangFanType == false && self.isXingpaiId2HupaiId == false && laizi[j].val == xingPaiId) {
                                self.isXingpaiId2HupaiId = true;
                                mz.isXingpaiId2HupaiId = true;
                            }
                        }
                    }
                }
            }
        }

        //处理非type2的赖子
        for (var i = retMenzi.length -1; i >= 0; i--){
            if (wangfanLocation == i) continue;
            if (retMenzi[i].isOpenCards == true) continue;
            replaceLaiziWithoutType2(retMenzi[i]);
        }

        return [retMenzi, wangfanLocation];
    },
    /**
     * 获得每个门子的位置
     * @param menzi
     */
    getComboPosNode : function(menzi){
        var self = this;
        var posNodeArr = [];
        var nodeWidth = 60;
        var len = menzi.length;
        var offsetX = -(nodeWidth * len / 2) + 30;
        var lianpai = self.$("container_data.container_personal.mid.lianpai");
        var cloneNode = self.$("container_data.container_personal.mid.lianpai.node");

        for (var i = 0; i < len; i++){
            posNodeArr[i] = duplicateNode(cloneNode);
            lianpai.addChild(posNodeArr[i]);
            posNodeArr[i].setPosition(cc.p(i * 60 + offsetX, 0));
        }

        cloneNode.setVisible(false);

        return posNodeArr;
    },
    /**
     * 设置门子
     * @param row
     * @param index
     * @param comboInfo
     * @param ByCary
     * @param wangfanLocation
     * @param isWanfanLocation
     * @param isXingpaiId2HupaiId
     */
    setComBoInfo: function (posNode, comboData, hupaiId, wangfanLocation, isWanfanLocation, isXingpaiId2HupaiId) {
        //setComBoInfo: function (row, index, comboInfo,ByCard) {
        var self = this;
        var pos = posNode.getChildByName('pos');
        var paiArr = comboData.pai_arr;
        var isHuId = false ;
        for (var i = 0; i < paiArr.length; i++) {
            var cardNo = paiArr[i];
            var card = new Card();
            card.setData(cardNo, null, 2);
            card.setAnchorPoint(0.5, 0.5);
            card.setScale(self._cardSize / 42);
            card.setPosition(0, i * self._cardSize);
            if (isXingpaiId2HupaiId == true) {
                if (hupaiId && hupaiId == cardNo && !isHuId && self.existHuDui == false && comboData.isXingpaiId2HupaiId == true) {
                    card.setHuMark(self._cardSize);
                    self.existHuDui = true;
                }
            }
            else {
                if (hupaiId && hupaiId == cardNo && !isHuId && self.existHuDui == false && wangfanLocation == -1) {
                    card.setHuMark(self._cardSize);
                    self.existHuDui = true;
                }
                if (hupaiId && hupaiId == cardNo && !isHuId && self.existHuDui == false && wangfanLocation > -1 && isWanfanLocation == true) {
                    card.setHuMark(self._cardSize);
                    self.existHuDui = true;
                }
            }
            pos.addChild(card);
        }
        if (paiArr.length > 2) {
            posNode.getChildByName('type').setVisible(true);
            posNode.getChildByName('huxi').setVisible(true);
            //posNode.getChildByName('type').setString(self.PAI_TYPE[comboData.type]);
            self.setUIImageByName(posNode.getChildByName('type'), self.PAI_TYPE[comboData.type]);
            posNode.getChildByName('huxi').setString(comboData.huXi);
        }
        else {
            posNode.getChildByName('type').setVisible(false);
            posNode.getChildByName('huxi').setVisible(false);
        }
    },
    /**
     * 设置头像
     * @private
     */
    _setRightInfo : function(){
        var self = this;
        var right = self.$("container_data.container_personal.right");
        var player;
        var players = self._data['players'];
        for (var i = 0; i < players.length; i++) {
            if (players[i]['hu']) {
                player = players[i];
                break;
            }
        }
        var huxi = 0;
        for (var i = 0; i < player.duiArr.length; i++){
            huxi += player.duiArr[i].huXi;
        }

        self.$("huxi_val", right).setString(huxi);
        self.$("tunshu_val", right).setString(player.win_tun);
        self.$("fanshu_val", right).setString(player.win_fan);
        self.$("zongtunshu_val", right).setString(player.win_tun * player.win_fan);
    },
    /**
     * 设置显示信息右侧的数据
     * @private
     */
    _setPlayerInfo : function(){
        var self = this;
        var user = self.$("container_data.container_user");
        var players = self._data.players;
        var zhuangID = self._data.zhuang;
        var head;
        var userData;
        for (var i = 0; i < 4; i++) {
            head = self.$("headNode" + i, user);
            if (i < players.length) {
                userData = gameData.getPlayerInfoByUid(players[i].uid);
                loadHeadImage(decodeURIComponent(userData.headimgurl), self.$("head", head), true);//头像
                self.$("playerName", head).setString(ellipsisStr(userData.nickname, 6));
                self.$("score", head).setString(players[i].score);
                //庄家
                if (zhuangID != players[i].uid)
                {
                    self.$("zhuang", head).setVisible(false);
                }
                //自己
                if (gameData.uid == players[i].uid) {
                    self.$("playerName", head).setTextColor(cc.color(255, 255, 0));
                    self.$("score", head).setTextColor(cc.color(255, 255, 0));
                }
            }
            else {
                head.setVisible(false);
            }
        }

    },
    /**
     * 设置按钮状态
     */
    _setBtn: function () {
        var self = this;
        var panelResult = self.$('PanelResult');
        panelResult.setVisible(true);
        var touchLayer = self._room.getTouchLayer();

        var checkResult = self.$('btn_jiesuan', panelResult);
        //var showTable = self.$('btn_zhuomian', panelResult);

        var jixu = self.$('continue', panelResult);

        TouchUtil.setOnclickListener(checkResult, function ()
        {
            self._room.showXiaojieZhuomain();
            cc.eventManager.pauseTarget(touchLayer, true);
            self.removeFromParent();
        });
        /*
        TouchUtil.setOnclickListener(showTable, function () {
            self._room.showXiaojieZhuomain();
            cc.eventManager.pauseTarget(touchLayer, true);
            self.removeFromParent();
        });
         */
        TouchUtil.setOnclickListener(jixu, function () {
            if (ZiPaiRoomData.isReplay) {
                self.removeFromParent();
            } else {
                self._room.showRoomClean();
                self._room.onReady();
                self.removeFromParent();
            }
        });
    },
    /**
     * 设置底牌
     * @private
     */
    _setDipai: function () {
        var self = this;
        var pais = self._data.dipai_arr;
        var paiLen = pais.length;
        var dipai = self.$("container_data.container_dipai");
        var txt_dipai = self.$("txt_dipai", dipai);
        var pos = self.$("pos", dipai);
        var lineNum = 1; // 1 2
        var maxNum = 15; // 15 20 27
        var cardSizeType = 2;
        var num1max = 15;// 1  42  15
        var num2max = 40;// 2  34  20
        // 2 34 27
        var offsetX = 0;
        var offsetY = 0;
        //设置摆放类型
        if (paiLen <= num1max){
            lineNum = 1;
            maxNum = paiLen;
            cardSizeType = 2;
        }
        else if (paiLen <= num2max){
            lineNum = 2;
            maxNum = 20;
            cardSizeType = 3;
        }
        else if (paiLen > num2max){
            lineNum = 2;
            maxNum = 27;
            cardSizeType = 3;
        }
        else {
            return;
        }

        //设置偏移值
        if (lineNum == 1){
            //90是"底牌"的位置
            offsetX = (90 + maxNum * 42) / 2;
            offsetY = 0;
        }
        else{
            offsetX = (90 + maxNum * 34) / 2;
            offsetY = 34 / 2;
        }

        txt_dipai.setPositionX(1280 / 2 - offsetX);
        pos.setPositionX(1280 / 2 - offsetX + 90);

        //创建卡
        for (var i = 0; i < paiLen; i++) {
            var card = new Card();
            card.setData(pais[i], null, cardSizeType == 2 ? 2 : 3);
            pos.addChild(card);
            card.setAnchorPoint(0, 0.5);
            if (i < maxNum) {
                if (cardSizeType == 2){
                    card.setPosition(cc.p(i * 42, 0));
                }
                else {
                    card.setPosition(cc.p(i * 34, offsetY));
                }
            }
            else {
                card.setPosition(cc.p((i - maxNum) * 34, -offsetY));
            }
        }
    },
    setUIImageByName : function (sprite, name) {
        var self = this;
        var imgName = self.name2img[name];
        var frame = cc.spriteFrameCache.getSpriteFrame(imgName);
        if (!frame) {
            cc.log('setUIImageByName ' + imgName);
            cc.spriteFrameCache.addSpriteFrames(zipai.res.ZiPai_JieSuan_font);
            var frame = cc.spriteFrameCache.getSpriteFrame(imgName);
            if (!frame){

            }
            else {
                sprite.initWithSpriteFrameName(imgName);
                return;
            }
        } else {
            sprite.initWithSpriteFrameName(imgName);
            return;
        }
        if (!cc.sys.isNative)
            throw new Error('getSpriteFrame(' + imgName + ') == null');
        else
            showAlert('getSpriteFrame(' + imgName + ') == null');
    },
    _hidePlayerData : function () {
        var self = this;
        self.$("container_data.container_personal").setVisible(false);
        self.$("container_data.container_user").setVisible(false);
    }
});